package com.qdlc.p2p.biz.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.UserFreezeService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.constant.LogTplNid;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.OperationLogDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dao.UserFreezeDao;
import com.qdlc.p2p.dal.dto.OperationLog;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserFreeze;
import com.qdlc.p2p.dal.model.UserFreezeModel;

@Service("freezeService")
public class UserFreezeServiceImpl implements UserFreezeService {

    @Resource
    private UserFreezeDao userFreezeDao;

    @Resource
    private UserDao userDao;

    @Resource
    private OperationLogDao operationLogDao;

    @Override
    public PageDataList<UserFreezeModel> freezeList(int pageNumber, int pageSize, UserFreezeModel model) {
        return userFreezeDao.freezeList(pageNumber, pageSize, model);
    }

    @Override
    public Map<String, Object> freezeAdd(UserFreezeModel model, Operator operator) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (model.getMark() == null) {
            map.put("result", false);
            map.put("msg", "冻结点不能为空！");
            return map;
        }
        if (!StringUtil.isBlank(model.getUserName())) {
            model.setMark(model.getMark().replaceAll(" ", ""));
            User user = userDao.getUserByUserName(model.getUserName().trim());
            if (user == null) {
                map.put("result", false);
                map.put("msg", "该用户(" + model.getUserName() + ")不存在,添加冻结信息失败！");
                return map;
            }
            if (userFreezeDao.isExistsByUserName(model.getUserName())) {
                map.put("result", false);
                map.put("msg", "该用户(" + model.getUserName() + ")的冻结信息已存在,请勿重复添加！");
                return map;
            }
            model.setUserId(user.getUserId());
        }
        model.setAddTime(DateUtil.getNow());
        model.setAddIp(Global.getIP());
        UserFreeze freeze = model.prototype();
        userFreezeDao.save(freeze);

        User user = userDao.getUserByUserName(model.getUserName());
        OperationLog operationLog = new OperationLog(user, operator, LogTplNid.FREEZE);
        operationLog.setOperationResult("用户名为" + operator.getUserName() + "的操作员冻结" + user.getUserName() + "的"
                + model.getMark() + "操作");
        operationLogDao.save(operationLog);
        map.put("result", true);
        map.put("msg", "保存成功！");
        return map;

    }

    @Override
    public UserFreeze find(long id) {
        return userFreezeDao.find(id);
    }

    @Override
    public void freezeEdit(UserFreezeModel model, Operator operator) {
        if (model.getMark() == null) {
            throw new UserException("冻结点不能为空！", 1);
        }
        model.setMark(model.getMark().replaceAll(" ", ""));
        UserFreeze userFreeze = userFreezeDao.find(model.getId());
        userFreeze.setMark(model.getMark());
        userFreeze.setStatus(model.getStatus());
        userFreeze.setRemark(model.getRemark());
        User user = userDao.getUserByUserName(model.getUserName());
        OperationLog operationLog = new OperationLog(user, operator, LogTplNid.FREEZE);
        operationLog.setOperationResult("用户名为" + operator.getUserName() + "的操作员修改冻结" + user.getUserName() + "的"
                + model.getMark() + "操作");
        operationLogDao.save(operationLog);

    }

    @Override
    public void freezeDelete(long id, int status) {
        userFreezeDao.freezeDelete(id, status);
    }

    @Override
    public UserFreeze getByUserId(long userId) {
        return userFreezeDao.getByUserId(userId);
    }
}
